<?php

namespace App\Console\Commands\model;

use App\Dep\ModelDep;
use App\Dep\SystemDep;
use App\Dep\TrainLogDep;
use App\Enum\CommonEnum;
use App\Service\UidService;
use Illuminate\Console\Command;
use GuzzleHttp\Client;

class DoTagCmd extends Command
{

    //打标
    public $signature = 'model:do-tag';

    public function handle()
    {
        $dep = new ModelDep();
        $logDep = new TrainLogDep();

        $resTagIng = $dep->firstTagIng();
        if ($resTagIng) {
            $this->log('already tag ing', ['id' => $resTagIng->id]);;
            return false;
        }

        $resWaitTag = $dep->getWaitTag();
        if ($resWaitTag->isEmpty()) {
            $this->log('have no wait tag');;
            return false;
        }

        $systemDep = new SystemDep();
        $resSystem = $systemDep->first();

        $batchNo = UidService::tagBatchUid();
        $tagDir = "{$resSystem->tag_dir}\\{$batchNo}";

        mkdir($tagDir);
        $this->log('create tag dir',['tag_dir'=>$tagDir]);

        $client = new Client();
        $resWaitTag->each(function ($item) use ($resSystem, $tagDir, $dep, $logDep, $client,$batchNo) {
            $sourceArr = json_decode($item->source, true);
            foreach ($sourceArr as $source) {
                $originPath = $resSystem->path . '\\' . '训练模型' . '\\' . $item->uuid . '\\素材图' . '\\' . $source['source'];
                $distPath = $tagDir . '\\' . $source['source'];
                if (copy($originPath, $distPath)) {
                    $this->log('copy success', ['originPath' => $originPath, 'targetPath' => $distPath]);
                } else {
                    $this->log('copy error', ['originPath' => $originPath, 'targetPath' => $distPath]);
                }
            }

            $data = [
                'is_tag' => CommonEnum::YES,
                'tag_at' => Date('Y-m-d H:i:s'),
                'tag_batch' => $batchNo
            ];
            $dep->edit($item->id, $data);

            $logData = [
                'content' => "开始打标,打标批次{$batchNo}",
                'model_id' => $item->id,
            ];
            $logDep->add($logData);
        });

        $jsonData =  [
            "path" => str_replace('\\','\\\\',$tagDir),
//            "path" => $resSystem->tag_dir,
            "interrogator_model" => "wd14-convnextv2-v2",
            "threshold" => "0.35",
            "additional_tags" => "",
            "exclude_tags" => "",
            "escape_tag" => true,
            "batch_input_recursive" => false,
            "batch_output_action_on_conflict" => "ignore",
            "replace_underscore" => true,
            "replace_underscore_excludes" => '0_0, (o)_(o), +_+, +_-, ._., <o>_<o>, <|>_<|>, =_=, >_<, 3_3, 6_9, >_o, @_@, ^_^, o_o, u_u, x_x, |_|, ||_||'
        ];
        $this->log('request server',$jsonData);
        $response = $client->request('post',$resSystem->tag_domain . '/api/interrogate', [
            'json' => $jsonData
        ]);

        $body = $response->getBody();
        $res = json_decode($body, true);
//        dd($res);
        $this->log('request server',$res);
    }

    private function log($msg, $context = [])
    {
        $this->info("do tag log: {$msg} " . json_encode($context,JSON_UNESCAPED_UNICODE));
        log_daily("do-tag")->info($msg, $context);
    }


}
